
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 29 10:21:04 2019

@author: maggieliu
"""

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math
from decimal import Decimal



#参数：
#参数取值范围：1.7-3.2

#指标缺失值转化为0
def null_trans(index_name):
    if index_name == u"缺失值":
        return 0.0
    else:
        try:
            value = float(index_name)
        except:
            value = 0.0
        return value

def cigarette_rating(x):
    if x>=380000:
        return "A"
    if x>=280000 and x<380000:
        return "B"
    if x>=200000 and x<280000:
        return "C"
    if x>=100000 and x<200000:
        return "D"
    if x>=70000 and x<100000:
        return "E"
    if x>=35000 and x<70000:
        return "F"
    if x>=10000 and x<35000:
        return "G"
    else:
        return u"缺失值"



#评级等级
rating_level = decisionModelResult.get("ratingLevel").lower()
#ST为短模型、LT为长模型
#AvgMoney(_LT)月均订烟额
def cigarette_level(x):
    if x =="ST":
        rating =null_trans({AvgMoney})
        return rating
    if x =="LT":
        rating = null_trans({AvgMoney_LT})
        return rating
    else:
        return u"缺失值"

cigarette_amount = cigarette_level({STLT_Check})


cigarette_amount = null_trans(cigarette_amount)
cigarette_level = cigarette_rating(cigarette_amount)



credit_line_dict = {"A":{'a':3.2,'b':3.1,'c':3,'d':2.9,'e':0},\
                    "B":{"a":3,"b":2.9,"c":2.8,"d":2.7,'e':0},\
                    "C":{'a':2.8,'b':2.7,'c':2.6,'d':2.5,'e':0},\
                    "D":{"a":2.6,"b":2.5,"c":2.4,"d":2.3,'e':0},\
                    "E":{"a":2.4,"b":2.3,"c":2.2,"d":2.1,'e':0},\
                    "F":{"a":2.2,"b":2.1,"c":2,"d":1.9,'e':0},\
                    "G":{"a":2,"b":1.9,"c":1.8,"d":1.7,'e':0},\
                    u"缺失值":{"a":0,"b":0,"c":0,"d":0,'e':0}}


#根据经营年限调整贷款额度上限
def loan_limit(x):
    if 1 <= x < 2:
        amount_limit = 300000
        return amount_limit
    elif x >= 2:
        amount_limit = 500000
        return amount_limit
    else:
        return 0

#额度矩阵
#申请额度
request_amount = float(ratingRequest.get('amount'))
#取烟商评级，信用评级，得到风险系数
def rating_adjust(x):
    if x == 'e':
        amount = 0
        return amount
    else:
        amount_index = credit_line_dict[cigarette_level][x]
        credit_amount = (Decimal(str(cigarette_amount))*Decimal(str(amount_index)))//1000*1000
        Ent_OperationPeriod_Short1=null_trans({R05_EG_EntRegisterCheck})
        amount_limit = loan_limit(Ent_OperationPeriod_Short1)
        amount = math.floor(min(min(credit_amount,amount_limit),request_amount))
        return amount

result = rating_adjust(rating_level)

